package com.busfm.service;

import java.util.HashMap;
import java.util.Map;

import com.busfm.activity.HomeActivity;
import com.busfm.R;
import com.busfm.download.DownloadJob;
import com.busfm.download.DownloadJobListener;
import com.busfm.download.DownloadThread;
import com.busfm.model.DownloadEntity;
import com.busfm.util.Constants;
import com.busfm.util.DBHelper;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class DownloadService extends Service {
	private NotificationManager notificationManager;
	private Map<String, DownloadThread> taskMap;
	private static final int DOWNLOAD_NOTIFY_ID = 999999;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		taskMap = new HashMap<String, DownloadThread>();
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);

		if (null == intent) {
			return;
		}

		if (intent.getAction().equals(Constants.ACTION_ADD_TO_DOWNLOAD)) {
			DownloadEntity downloadEntity = (DownloadEntity) intent
					.getSerializableExtra(Constants.EXTRA_KEY_DOWNLOAD_ENTITY);
			addToDownloadQueue(downloadEntity, startId);
		}
	}

	public void addToDownloadQueue(DownloadEntity downloadEntity, int startId) {
		if (null == downloadEntity) {
			return;
		}

		DownloadJob downloadJob = new DownloadJob(downloadEntity, startId);
		if (null != taskMap && taskMap.containsKey(downloadEntity.songId)) {
			taskMap.remove(downloadEntity.songId).stopDownloadThread();
		}

		if (null != DBHelper.getDownloadEntity(downloadEntity.songId)) {
			return;// Allready Downloaded ,No need to download again
		}

		downloadJob.setListener(mCallBack);
		DownloadThread downloadThread = new DownloadThread(downloadJob);
		downloadThread.start();
		taskMap.put(downloadEntity.songId, downloadThread);
		DBHelper.addToDownloadDB(downloadEntity);
	}

	DownloadJobListener mCallBack = new DownloadJobListener() {

		@Override
		public void downloadStarted(DownloadJob job) {

		}

		@Override
		public void downloadCompleted(DownloadJob job) {
			displayNotifcation(job);
		}

		@Override
		public void downloadFatal(DownloadJob job) {

		}
	};

	private void displayNotifcation(DownloadJob job) {

		String notificationMessage = job.getDownloadEntity().title + " "
				+ job.getDownloadEntity().artist;

		Notification notification = new Notification(
				android.R.drawable.stat_sys_download_done, notificationMessage,
				System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, HomeActivity.class), 0);

		notification.setLatestEventInfo(this, getString(R.string.downloaded),
				notificationMessage, contentIntent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		notificationManager.notify(DOWNLOAD_NOTIFY_ID, notification);
	}
}
